<!DOCTYPE html>
<html lang="en">
<head>
    <title>公告管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <!--    <meta http-equiv="Access-Control-Allow-Origin" content="*">-->
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css"/>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<!--搜索框-->
<div class="demoTable">
    名称：
    <div class="layui-inline">
        <input class="layui-input" name="name" id="name" autocomplete="off">
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>
<!-- 自定义的控件，表格左上角位置按钮 -->
<div id="toolbar" style="display: none; ">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add" data-method="notice" data-type="auto">添加</button>
    </div>
</div>
<!--操作列表按钮-->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--添加修改表单，通过拟态框来见实现-->
<div id="modal" style="display:none">
    <form class="layui-form" id="user-add" action="" lay-filter="entity">
        <input type="text" name="id" class="layui-input" style="display:none">
        <div class="layui-form-item">
            <label class="layui-form-label">公告标题1：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="title" required lay-verify="required" placeholder="公告标题"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告内容：</label>
            <div class="layui-input-block">
                <textarea placeholder="公告内容" name="contents" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告状态：</label>
            <div class="layui-input-inline">
                <input type="radio" name="annStatus" value="0" title="正常" checked="">
                <input type="radio" name="annStatus" value="1" title="废弃">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button style="display: none;" type="reset" class="layui-btn layui-btn-primary swiper-right-form-reset">
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

<script src="/layui/layui.js" charset="utf-8"></script>

<script>
    layui.use(['table', 'layer', 'form', 'jquery'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        table.render({
            elem: '#test'
            , url: '/api/announcement/page'
            // , height: document.body.clientHeight - 180
            , page: true //开启分页
            , id: 'testReload'
            , cols: [[
                {field: "id", title: "ID"},
                {field: "title", title: "标题"},
                {field: "contents", title: "内容"},
                {
                    field: "annStatus", title: "状态", templet: function (d) {
                        return d.annStatus === 0 ? "正常" : "废弃";
                    }
                },
                {
                    field: "createDatetime",
                    title: "创建时间",
                    templet: "<div>{{layui.util.toDateString(d.createTime,'yyyy-MM-dd HH:mm:ss')}}</div>"
                },
                {title: '操作', width: 150, toolbar: '#barDemo', align: 'center'}
            ]]
            , toolbar: '#toolbar',//表格头信息
            response: {	// 设置响应码
                statusCode: 1 //规定成功的状态码，默认：0
            },
            parseData: function (rs) {//数据格式解析
                if (rs.status == 1) {
                    return {
                        "code": rs.status,
                        "msg": rs.message,
                        "count": rs.total,
                        "data": rs.data
                    }
                }
            }
        });
        //监听事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    open2("添加页面", null);
                    break;
            }
            ;
        });
        //监听工具条
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    layer.confirm('真的删除该条信息吗？', function (index) {
                        $.ajax({
                            type: "delete",
                            url: "/api/announcement/" + data.id,
                            dataType: "json",
                            success: function (da) {
                                if (da.status == 1) {
                                    layer.msg(da.message)
                                    obj.del(); //删除对应行（tr）的DOM结构
                                } else {
                                    layer.msg(da.message)
                                }
                            },
                            error: function (da) {
                                alert("出现错误");
                                return false;
                            }
                        }) //ajax结束
                        layer.close(index);
                    });
                    break;
                case 'edit':
                    open2("修改页面", obj.data);
                    break;
            }
        });

        //"模态框",
        function open2(title, entity) {
            form.val("entity", entity);
            layer.open({
                type: 1
                , title: title
                , content: $("#modal")
                , offset: '10px'
                , area: ['600px', '80%']
                , end: function () {
                    $('.swiper-right-form-reset').click()
                    table.reload('testReload');
                }
            });
        }

        //监听提交
        form.on('submit(formDemo)', function (data) {
            var data1 = form.val("entity");
            var type = data1.id ? "put" : "post";
            console.log(type);
            $.ajax({
                url: '/api/announcement',
                type: `${type}`,
                dataType: "json",
                contentType: 'application/json',
                data: JSON.stringify(data1),
                success: function (da) {
                    if (da.status == 1) {
                        // layer.alert(da.message, function () {
                        layer.msg(da.message, {time: 1 * 1000}, function () {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(da.message);
                    }
                }
            });
            return false;
        });

        var $ = layui.$, active = {
            reload: function () {
                var name = $('#name');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "title": name.val()
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>